/**
 * 
 */
package com.cr.backbone.ajax;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import com.cr.backbone.util.UtilsDataDict;
import com.opensymphony.xwork2.ActionSupport;
import com.readnote.book.model.Book;
import com.readnote.book.model.UserBook;
import com.readnote.book.model.UserNote;
import com.readnote.book.service.BookService;
import com.readnote.book.service.UserBookService;
import com.readnote.book.service.UserNoteService;
import com.readnote.user.model.User;
import com.readnote.user.service.UserService;

/**
 * 通用Ajax 下拉框数据获取action
 * 
 * @project RaedNote
 * @version 1.0
 * @company
 * @fileDescribe
 * @author xianghua.hu
 * @date 2012-5-16 下午04:24:43
 */
/**
 * @author huxh
 * 
 */
public class AjaxOptionsAction extends ActionSupport {
	private static final long serialVersionUID = -6786049821588908063L;


	private static final String EMPTY = "            ";
	
	
	// Ajax Select Options
	// 通过ajax获取的数据集合
	private Collection<AjaxSelectOption> options = new ArrayList<AjaxSelectOption>();
	private Long parentId;// 父级ID，根据此ID，进行子集数据查询

	// 业务Service
	// *****************************************
	// ** 注意！！！！！
	// json action对象中的 service方法，只需要set；如果写入get那么整个action 序列化json对象会出错
	private BookService bookService;
	private UserService userService;
	private UserBookService userBookService;
	private UserNoteService userNoteService;


	private UserNote usernote=null;
	
	/**
	 * 课程
	 * @author xianghua.hu
	 * @date 2012-5-17
	 */
	@Deprecated
	public String ajaxBooks_delete_20120605_huxh() {
		Collection<Book> books = bookService.getList();
		options = new ArrayList<AjaxSelectOption>();
		options.add(new AjaxSelectOption("",EMPTY));//空选项
		for (Book b : books) {
			AjaxSelectOption op = new AjaxSelectOption();
			String value = String.valueOf(b.getId());
			String text = b.getTitle();
			op.setText(text);
			op.setValue(value);
			options.add(op);
		}
		
		return SUCCESS;
	}
	
	
	

	/**
	 * 数据字典 
	 * @author xianghua.hu
	 * @date 2012-5-31
	 */
	@Deprecated
	public String ajaxDataDictYesNo_delete_20120605_huxh() {
		Integer[] dataDicts = UtilsDataDict.YES_OR_NO;
		Integer t=UtilsDataDict.YES;
		options = new ArrayList<AjaxSelectOption>();
		options.add(new AjaxSelectOption("",EMPTY));//空选项
		for(Integer data: dataDicts){
			AjaxSelectOption op = new AjaxSelectOption();
			String value = String.valueOf(data);
			String text = getText(value);
			op.setText(text);
			op.setValue(value);
			options.add(op);
		}
		return SUCCESS;
	}
	
	
	/**
	 * 实例
	 * 课程
	 * @author xianghua.hu
	 * @date 2012-5-17
	 */
	public String ajaxBooks() {
		//1、 获取数据
		Collection books = bookService.getList();
		
		//2、指定构成option Key\value 的 object 属性名
		Collection<Object> values=books;
		String textKey="title";
		String valueKey="id";
		
		//3、自动初始化并返回
		options = UtilsOptionsInit.initCollectionByObject(values, textKey, valueKey);
		
		return SUCCESS;
	}
	
	/**
	 * 数据字典 
	 * @author xianghua.hu
	 * @date 2012-5-31
	 */
	public String ajaxDataDictYesNo() {
		//1、获取数据字典
		Integer[] dataDicts = UtilsDataDict.YES_OR_NO;

		//2、准备数据
		Collection<Integer> values = Arrays.asList( dataDicts );
		
		//3、自动初始化并返回
		options = UtilsOptionsInit.initCollectionByDataDict(values);
		return SUCCESS;
	}
	
	

	/**
	 * 用户集合
	 * @author xianghua.hu
	 * @date 2012-5-31
	 */
	public String ajaxUsers() {
		Collection<User> users = userService.getList();
		options = new ArrayList<AjaxSelectOption>();
		options.add(new AjaxSelectOption("",EMPTY));//空选项
		for(User user: users){
			AjaxSelectOption op = new AjaxSelectOption();
			String value = String.valueOf(user.getId());
			String text = getText(user.getLoginName());
			op.setText(text);
			op.setValue(value);
			options.add(op);
		}
		return SUCCESS;
	}
	
	
	/**
	 * 用户书籍集合
	 * @author xianghua.hu
	 * @date 2012-5-31
	 */
	public String ajaxUserBooks() {
		
		options = new ArrayList<AjaxSelectOption>();
		options.add(new AjaxSelectOption("",EMPTY));//空选项
		
		if(parentId == null || parentId <=0 )
			return SUCCESS;
		
		User user=new User(parentId);
		Collection<UserBook> userBooks= userBookService.getListUserBookByUser(user);
		for(UserBook userBook: userBooks){
			AjaxSelectOption op = new AjaxSelectOption();
			String value = String.valueOf(userBook.getId());
			String text = getText(userBook.getBook().getTitle());
			op.setText(text);
			op.setValue(value);
			options.add(op);
		}
		return SUCCESS;
	}
	
	
	/**
	 * 用户书籍笔记集合
	 * @author xianghua.hu
	 * @date 2012-5-31
	 */
	public String ajaxUserNotes() {
		options = new ArrayList<AjaxSelectOption>();
		options.add(new AjaxSelectOption("",EMPTY));//空选项
		
		if(parentId == null || parentId <=0 )
			return SUCCESS;
		
		UserBook userBook=new UserBook(parentId);
		Collection<UserNote> userNotes= userNoteService.getListUserNoteByUserBook(userBook);
		for(UserNote userNote: userNotes){
			AjaxSelectOption op = new AjaxSelectOption();
			String value = String.valueOf(userNote.getId());
			String text = getText(userNote.getTitle());
			op.setText(text);
			op.setValue(value);
			options.add(op);
		}
		return SUCCESS;
	}
	
	/**
	 * 用户书籍笔记内容
	 * @author xianghua.hu
	 * @date 2012-5-31
	 */
	public String ajaxUserNotesContent() {
		options = new ArrayList<AjaxSelectOption>();
		options.add(new AjaxSelectOption("",EMPTY));//空选项
		
		if(parentId == null || parentId <=0 )
			return SUCCESS;
		
		usernote=userNoteService.get(parentId);
		
		return SUCCESS;
	}
	

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	public Long getParentId() {
		return parentId;
	}

	public void setParentId(Long parentId) {
		this.parentId = parentId;
	}

	public Collection<AjaxSelectOption> getOptions() {
		return options;
	}

	public void setOptions(Collection<AjaxSelectOption> options) {
		this.options = options;
	}


	public void setUserService(UserService userService) {
		this.userService = userService;
	}


	public void setUserBookService(UserBookService userBookService) {
		this.userBookService = userBookService;
	}


	public void setUserNoteService(UserNoteService userNoteService) {
		this.userNoteService = userNoteService;
	}


	public UserNote getUsernote() {
		return usernote;
	}


	public void setUsernote(UserNote usernote) {
		this.usernote = usernote;
	}

}
